/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.mts.client.core.view;

import com.intelligent.mts.client.core.modal.HistoryModal;
import com.intelligent.mts.client.utils.GBC;
import com.intelligent.mts.core.dto.HistoryDto;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by Peter on 2016/7/21.
 */
@Component
public class MainHistoryPanel extends JPanel {

    private HistoryModal historyModal;

    public void reloadTable(List<HistoryDto> historyDtoList) {
        historyModal.setData(historyDtoList);
        historyTable.setModel(historyModal);
    }

    @PostConstruct
    private void initComponents() {

        String columns[] = new String[]{"Station Id", "Station Name", "PCBA SN", "Heat Sink SN", "Housing SN", "Time", "Result"};
        historyModal = new HistoryModal(columns);
        historyTable = new JTable();
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(SwingConstants.CENTER);
        historyTable.setDefaultRenderer(Object.class, tcr);
        scrollPane = new JScrollPane();

        this.setBorder(BorderFactory.createTitledBorder("History"));
        this.setLayout(new GridBagLayout());
        scrollPane.setEnabled(false);
        historyTable.setModel(historyModal);

        historyTable.getTableHeader().setVisible(true);
        historyTable.getColumnModel().getColumn(0).setPreferredWidth(10);
        TableColumn column = null;
        for (int i = 0; i < historyTable.getColumnCount(); i++) {
            column = historyTable.getColumnModel().getColumn(i);
            String headName = column.getHeaderValue().toString();
            if (headName.equals(columns[1])) {
                column.setPreferredWidth(200);
            } else if (headName.equals(columns[5])) {
                column.setPreferredWidth(150);
            } else {
                column.setPreferredWidth(50);
            }
        }
        scrollPane.setViewportView(historyTable);
        this.setLayout(new GridBagLayout());
        this.add(scrollPane, new GBC(0, 0).setFill(GBC.BOTH).setWeight(100, 100));
    }

    private JTable historyTable;
    private JScrollPane scrollPane;
}
